package com.bby.screentool.controller;

import com.bby.screentool.bean.Action;
import com.bby.screentool.service.ScreenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/screen")
public class ScreenController {
    @Autowired
    ScreenService screenService;

    @Value("${screen-api.key: zmnxcksldlngntuy3jj5n2omapir}")
    String key;

    @GetMapping("id")
    String getScreenId(HttpServletRequest request) {
        if (!key.equals(request.getHeader("token"))){
            return "forbidden";
        }
        return screenService.getScreenId();
    }

    @GetMapping("check")
    @ResponseBody
    Action checkScreenId(HttpServletRequest request, @RequestParam(name = "screen_id") String screenId) {
        if (!key.equals(request.getHeader("token"))){
            return new Action(Action.ERROR);
        }
        return screenService.checkScreenId(screenId);
    }

}
